package com.boodaba.todocarts.ui;

import java.util.List;

import com.boodaba.todocarts.R;
import com.boodaba.todocarts.app.ILabelActivity;
import com.boodaba.todocarts.app.LabelPresenter;
import com.google.inject.Inject;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import roboguice.activity.GuiceListActivity;
import roboguice.inject.InjectView;

public class LabelActivity extends GuiceListActivity implements ILabelActivity {
	@Inject protected LabelPresenter mPresenter;
	@InjectView(android.R.id.button1) protected ImageButton mAddLabelButton;
	
	protected String mSelectedTitle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_label);
		mPresenter.initializeWith(this);
		
		mAddLabelButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				mPresenter.startAddLabel();				
			}
		});
		
		getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> adapter, View v, int position, long id) {
				mSelectedTitle = (String) getListAdapter().getItem(position);
				mPresenter.startEditLabel();
				return true;
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.options_menu_label, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_add_label:
				mPresenter.startAddLabel();
				return true;
		}
		return false;
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		if (position < 0) {
			return;
		}
		mSelectedTitle = (String) getListAdapter().getItem(position);
		mPresenter.showLabelTodos();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		mPresenter.returnTo();
	}
	
	public void setLabelList(List<String> labelList) {
		List<String> titles = labelList;
		ArrayAdapter<String> titlesAdapter = new ArrayAdapter<String>(
				this,
				R.layout.list_item_main,
				android.R.id.text1,
				titles);
		setListAdapter(titlesAdapter);
	}

	public void startAddLabel() {
		Intent i = new Intent(this, LabelEditActivity.class);
		this.startActivityForResult(i, 0);
	}
	
	public void startEditLabel(String labelTitle) {
		Intent i = new Intent(this, LabelEditActivity.class);
		i.putExtra("labelTitle", labelTitle);
		this.startActivityForResult(i, 0);
	}

	public String getSelectedTitle() {
		return mSelectedTitle;
	}

	public void startLabelTodos(String labelTitle) {
		Intent i = new Intent(this, LabelTodosActivity.class);
		i.putExtra("labelTitle", labelTitle);
		this.startActivityForResult(i, 0);
	}
}
